/**
 * 编辑面
 */

import Edit from './Edit'
import IMG_CIRCLE_YELLOW from '../images/circle_yellow.png'


export default class EditCircle extends Edit {
  constructor(overlay) {
    super()
    this._overlay = overlay
    this._radius = 0
    this._positions = []
    this._message = '点击锚点移动,右击结束编辑'
  }

  _mountEntity() {
    this._radius = this._overlay.radius

    this._center = this._overlay.positions
    this._overlay.show = false
    this._delegate = new Cesium.Entity({
      polygon: {
        material: this._overlay.delegate?.polygon?.material,
        ...this._overlay._style
      }
    })
    
    this._positions = [].concat([
      this._center,
      this._computeCirclePoints(this._center, this._radius)[0]
    ])
    this._delegate.polygon.hierarchy = new Cesium.CallbackProperty(time => {
      if (this._positions.length > 1) {
        this._radius = Cesium.Cartesian3.distance(
          this._positions[0],
          this._positions[1]
        )
        if (this._radius <= 0) {
          return null
        }
        let pnts = this._computeCirclePoints(this._positions[0], this._radius)
        pnts.push(pnts[0])
        return new Cesium.PolygonHierarchy(pnts)
      } else {
        return null
      }
    }, false)
    this._layer.add(this._delegate)
  }

  _mountAnchor() {
    this._positions.forEach((item, index) => {
      this.createAnchor(item, index, false, index % 2 === 0)
    })
  }

  _computeCirclePoints(center, radius) {
    let pnts = []
    let cep = Cesium.EllipseGeometryLibrary.computeEllipsePositions(
      {
        center: center,
        semiMajorAxis: radius,
        semiMinorAxis: radius,
        rotation: 0,
        granularity: 0.005
      },
      false,
      true
    )
    if (cep && cep.outerPositions) {
      pnts = Cesium.Cartesian3.unpackArray(cep.outerPositions)
    }
    return pnts
  }

  _onClick(e) {
    e = this.getMouseInfo(e.position)
    if (this._isMoving) {
      this._isMoving = false
      if (this._pickedAnchor && this._pickedAnchor.position) {
        let position = this._clampToGround ? e.surfacePosition : e.position
        if (!position) return
        this._anchorLayer.removeAll()
        this._anchors = []
        this._mountAnchor()
      }
      this._pickedAnchor = undefined
    } else {
      this._isMoving = true
      if (!e.target || !e.target.id) {
        return false
      }
      if (e.target.id.ellipse) return

      this._pickedAnchor = e.target.id
      this._pickedAnchor.billboard && this._pickedAnchor.billboard.image.setValue(IMG_CIRCLE_YELLOW)
    }
  }


  _onRightClick(e) {
    this.unbindEvent()
    this._overlay.positions = this._positions[0]
    this._overlay.radius = this._radius
    this._overlay.show = true
    this._plotEvent.raiseEvent(this._overlay)
  }

  /**
   *
   * @param position
   * @param index
   * @param isMid
   * @param isCenter
   */
   createAnchor(position, index, isMid = false, isCenter = false) {
    let image = isMid
      ? this._options.icon_midAnchor
      : isCenter
        ? this._options.icon_center
        : this._options.icon_anchor
    let anchor = this._anchorLayer.add({
      position: position,
      billboard: {
        image: image,
        width: 12,
        height: 12,
        eyeOffset: new Cesium.Cartesian3(0, 0, -500)
      },
      properties: {
        isMid: isMid,
        index: index,
        message : isMid ? '拖动此点修改半径' : '拖动此点编辑位置'
      }
    })
    this._anchors.push(anchor)
  }
}

